const express = require('express');
const app = express()
const mysql = require('mysql');
const chatTextList = require('./chatText.js')

 var http = require('http');

  var appkey = 'BC-1ec756cabfc142959d48fa83cce60e23';
  var channel = 'muge';
  var content = 'Hello, GoEasy!';

  var queryParams = 'appkey='+appkey+'&channel='+channel+'&content='+content;

  var options = {
    hostname: 'hangzhou.goeasy.io',
    path: '/publish',
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  };

  var req = http.request(options, (res) => {
    res.setEncoding('utf8');
    res.on('data', (result) => {
      console.log(`响应结果: ${result}`);
    });
  });

  req.on('error', (e) => {
    console.error(e);
  });

  req.write(queryParams);
  req.end();



const connection = mysql.createConnection({
	host: 'localhost',
	user: 'root',
	password: '1161105403',
	database: 'chat'
});
connection.connect();
const getUserCaht = () => {
	connection.query('SELECT * from user', function(err, data, fields) {
		if (err) {
			console.log(err);
			return;
		};
		console.log(data);
		dataUser = data
	})
}
// getUserCaht()
//设置允许跨域访问该服务.
// app.all('*', function(req, res, next) {
// 	res.header('Access-Control-Allow-Origin', '*');
// 	res.header('Access-Control-Allow-Headers', 'Content-Type');
// 	res.header('Access-Control-Allow-Methods', '*');
// 	res.header('Content-Type', 'application/json;charset=utf-8');
// 	next();
// });
app.get('/', (req, res) => res.send('Hello World!'))
app.get('/getUserChat', (req, res) => {
	res.send(chatTextList.userText)
})
app.get('/getServiceChat', (req, res) => {
	res.send(chatTextList.serviceText)
})
connection.end();
app.listen(3000, () => console.log('Example app listening on port 3000!'))
